package com.wenda.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wenda.entity.User;
import com.wenda.service.UserService;

@Controller
public class LoginController {
	
	@Autowired
	UserService userService;
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String getUser(String username,String password,HttpServletRequest request) {
		//查询用户信息
		User user = userService.getUser(username,password);
		//结果判断
		if(user == null){
			request.setAttribute("error", "用户名或密码错误");
			return "login";
		}
		//获取Session，保存用户信息至session域
		HttpSession session = request.getSession();
		session.setAttribute("user", user);
		//学生、教师、企业
		Integer roleId = user.getRoleId();
		if(roleId <= 3) {
			return "redirect:/submit";
		}else if(roleId <= 5) {
			return "redirect:/check";
		} else {
			return "redirect:/admin";
		}
	}
}
